//
//  getLocation.m
//  微他
//
//  Created by 微我网络 on 14-9-11.
//  Copyright (c) 2014年 微我网络. All rights reserved.
//

#import "GetLocation.h"
#import <AFNetworking.h>

@interface GetLocation ()<BMKLocationServiceDelegate>

@property (nonatomic,strong)BMKLocationService *loacation;

@end

@implementation GetLocation

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _loacation = [[BMKLocationService alloc] init];
        _loacation.delegate = self;
    }
    return self;
}
//
#pragma mark --百度定位

//定位
- (void)startLocation {
    
    _loacation.delegate = self;
    [_loacation startUserLocationService];
}

#pragma mark --定位代理

- (void)didFailToLocateUserWithError:(NSError *)error {
    
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_loacation stopUserLocationService];
    
    CLLocationCoordinate2D c2;
    c2.latitude = userLocation.location.coordinate.latitude;
    c2.longitude = userLocation.location.coordinate.longitude;
//    BMKMapPoint mp2 = BMKMapPointForCoordinate(c2);
    
    //获取经纬度地理信息
    CLGeocoder *Geocoder=[[CLGeocoder alloc] init];
    
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:c2.latitude longitude:c2.longitude];
    [Geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *place, NSError *error) {
        
        CLPlacemark *placemark = [place lastObject];
        NSLog(@"定位==%@,%@,%@,%@,%@,%@",
              placemark.administrativeArea,
              placemark.locality,
              placemark.subLocality,
              placemark.thoroughfare,
              placemark.subThoroughfare,
              placemark.name);
        NSString *postStr = [NSString stringWithFormat:@"http://api.map.baidu.com/telematics/v3/weather?location=%@&output=json&ak=A72e372de05e63c8740b2622d0ed8ab1",placemark.locality];
        postStr = [postStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",postStr);
        [[AFHTTPSessionManager manager] POST:postStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"%@",responseObject[@"message"]);
            if (responseObject[@"status"] && [responseObject[@"status"] isEqualToString:@"success"]) {
                
                NSArray *data = [responseObject[@"results"] lastObject][@"weather_data"];
                NSDictionary *dic = (NSDictionary *)[data firstObject];
                NSString *temp = dic[@"date"];
                if ([temp rangeOfString:@"实时"].length > 0) {
                    temp = [temp substringWithRange:NSMakeRange(14, temp.length-16)];
                    
                }else
                {
                    temp = dic[@"temperature"];
                    temp = [temp substringWithRange:NSMakeRange(0, temp.length-1)];
                }
                
                [_delegate requestSuccessWithCityName:placemark.locality weather:dic[@"weather"] temperature:temp];
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
        
    }];
}

@end
